//
//  EmoticonsPakage.swift
//  DEMO4
//
//  Created by J on 16/5/8.
//  Copyright © 2016年 J. All rights reserved.
//


/*
 1.加载 emoticons.plist 拿到每组表情的路径
 emoticons.plist(字典)  存储所有表情数据 -> 字典
 |---packages (字典数组）
 |---id(存储了对应组表情所在文件夹)
 
 
 
 2.根据拿到的路径 加载对应组表情的info.plist(字典)文件
 |---id(当前组表情文件夹名称)
 |---group_name_cn(组名称)
 |---emoticons(字典数组存储所有的表情)
 |---chs(表情对应文字)
 |---png(表情对应图片)
 |---code(emoji对应的十六进制字符串)
 
 
 */


import UIKit

class EmoticonsPakage: NSObject {
    
    var id : String?
    var group_name_cn : String?
    var emoticons : [Emoticon]?
    
    init (id:String){
        self.id = id
    }

    
    class func loadPackages() -> ([EmoticonsPakage]) {
        var packages = [EmoticonsPakage]()
        //0.创建最近组
        let pk = EmoticonsPakage(id: "")
        pk.group_name_cn = "最近"
        pk.emoticons = [Emoticon]()
        pk.appendEmptyEmoticons()
        packages.append(pk)
        
        
        //加载 moticons.plist
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil,inDirectory: "Emoticons.bundle")
        let dict = NSDictionary(contentsOfFile: path!)!
        
        //获取packages
        let dictArray = dict["packages"] as! [[String: AnyObject]]
        for d in dictArray{
            let package = EmoticonsPakage(id: d["id"]! as! String)
            packages.append(package)
            package.loadEmoticon()//遍历表情数组 并赋值给package的表情数组对象
            package.appendEmptyEmoticons()
            
        }
        
        return packages
    }
    
    
    //加载每一组所有的表情
    func loadEmoticon() {
        let emotionDic = NSDictionary(contentsOfFile: infoPath("info.plist") as String)!
        group_name_cn = (emotionDic["group_name_cn"] as! String)
        let dictArray = emotionDic["emoticons"] as! [[String: String]]
        
        emoticons = [Emoticon]()//初始化表情数组
        var index = 0
        for dict in dictArray {

            if index == 20 {//如果有20的情况直接添加一个删除按钮就行了
                emoticons?.append(Emoticon(isRemoveButton: true))
                index = 0
            }
            
            emoticons?.append(Emoticon(dict: dict, id: id!))
            index+=1
        }
        
//        print(emotionDic)
    }
    
    
    //追加空白按钮 如果一页不足21个 调用添加
    func appendEmptyEmoticons(){
        let count = emoticons!.count % 21 //缺几个
        for _ in count..<20 {//总共21
            //追加空白按钮
           emoticons?.append(Emoticon(isRemoveButton: false))
        }
        //追加删除按钮
        emoticons?.append(Emoticon(isRemoveButton: true))
//        print(count)
    }
    

    
    
    //获取微博表情路径
    
    class func emojitonPath() -> NSString {
        
        return (NSBundle.mainBundle().bundlePath as NSString).stringByAppendingPathComponent("Emoticons.bundle")
    }
    
    
    ///  获取指定文件全路径
    ///
    ///  - parameter fileName: 文件名称
    ///
    ///  - returns: 全路径
    func infoPath(fileName: String) -> NSString {
    
            return (EmoticonsPakage.emojitonPath().stringByAppendingPathComponent(id!) as NSString).stringByAppendingPathComponent(fileName)
    }

    
    
}



class Emoticon : NSObject {
    var code : String?{
        didSet{
            //1.从字符串中取出十六进制
            let scanner = NSScanner(string: code!)
            //2.将十六进制转换为字符串
            var result: UInt32 = 0
            scanner.scanHexInt(&result)//最后会把值赋给result
//            print(result) //9728
            //3.转换为unicode 字符串
             emojiStr = "\(Character(UnicodeScalar(result)))"
        }
    }
    var png : String?{
        didSet{
            imagePath = (EmoticonsPakage.emojitonPath().stringByAppendingPathComponent(id!) as NSString).stringByAppendingPathComponent(png!)
        }
    }
    var chs : String?
    var id : String?
    var imagePath : String?///表情图片全路径
    var emojiStr : String?//unicode emoji
    
    init(dict: [String: String], id: String){
        super.init()
        self.id = id
        setValuesForKeysWithDictionary(dict)
    }
    init (isRemoveButton: Bool){
        super.init()
        self.isRemoveButton = isRemoveButton
        
    }
    
    
    
    var isRemoveButton: Bool = false///标记是否为删除按钮
    
    
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
}
